<template>
	<view class="view">
		<serachInput @search="search" :value="keyWord"></serachInput>

		<view class="list" v-if="list.length != 0">
			<doctor-item v-for="(item, index) in list" :key="index" :item="item"></doctor-item>
		</view>
		<view v-if="list.length == 0" class="empty-view">没有结果</view>
	</view>
</template>

<script>
	import {
		selectDoctorByDisease,
		selectDoctorByKeyWord,
		selectDoctorByHospital
	} from '@/apis/request.js';
	import serachInput from '@/components/searchInput.vue';
	import DoctorItem from '@/components/doctor-item/doctor-item.vue';

	export default {
		components: {
			serachInput,
			DoctorItem
		},
		data() {
			return {
				list: [],
				cityId: '350200',
				finished: false,
				pageNo: 1,
				type: '',
				keyWord: ''
			};
		},
		methods: {
			to(url) {
				uni.navigateTo({
					url
				});
			},
			async search(e) {
				this.type = '';
				this.finished = false;
				this.pageNo = 1;
				this.list = [];
				this.keyWord = e;
				const params = {
					cityId: this.cityId,
					keyWord: this.keyWord,
					pageNo: this.pageNo++,
					pageSize: 10
				};
				const list = await selectDoctorByKeyWord(params);
				this.list.push(...list.records);
			},
			async getKeyword() {
				if (!this.finished) {
					const params = {
						cityId: this.cityId,
						keyWord: this.keyWord,
						pageNo: this.pageNo++,
						pageSize: 10
					};
					const data = await selectDoctorByKeyWord(params);
					this.list.push(...data.records);
					wx.stopPullDownRefresh();
					if (data.total == this.pageNo) {
						this.finished = true;
					}
				}
			},
			async getlist(diseaseId = '', departmentId = '', hospitalId = '') {
				if (!this.finished) {
					//diseaseId疾病id, departmentId科室id,hospitalId医院id,keyWord搜索关键字
					const params = {
						cityId: this.cityId,
						diseaseId: diseaseId,
						departmentId: departmentId,
						hospitalId: hospitalId,
						pageNo: this.pageNo++,
						pageSize: 10
					};
					let list = [];
					if (this.type == '1') {
						list = await selectDoctorByDisease(params);
					} else {
						list = await selectDoctorByHospital(params);
					}
					this.list.push(...list.records);
					uni.stopPullDownRefresh();
					if (list.records.length == 0) {
						this.finished = true;
					}
				}
			}
		},
		onReachBottom(options) {
			if (this.type == 2) {
				this.getlist(this.diseaseId, this.departmentId, this.hospitalId);
			} else {
				this.getKeyword();
			}
		},
		onPullDownRefresh(options) {
			this.finished = false;
			this.pageNo = 1;
			this.list = [];
			if (this.type == 2) {
				this.getlist(this.diseaseId, this.departmentId, this.hospitalId);
			} else {
				this.getKeyword();
			}
		},
		onShow() {
			const city = uni.getStorageSync('city');
			this.cityId = city ? city.id : '350200';
			if (this.type == 2) {
				this.getlist(this.diseaseId, this.departmentId, this.hospitalId);
			} else {
				this.getKeyword();
			}
		},
		onLoad(options) {
			this.keyWord = options.keyWord;
			this.type = options.type;
			this.diseaseId = options.diseaseId;
			this.departmentId = options.departmentId;
			this.hospitalId = options.hospitalId
		}
	};
</script>

<style lang="scss" scoped>
	.view {
		background-color: #fff;

		.empty-view {
			height: calc(100vh - 110upx);
		}
	}
</style>